﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bmz.Laboratory.Algorithm
{
    public class MoneyFromNumToWord
    {
        public static string MoenyToChineseWord(decimal money)
        {
            int moneyInt = (int)money;
            Dictionary<int, string> unit = new Dictionary<int, string>();
            unit.Add(2, "十");
            unit.Add(3, "百");
            unit.Add(4, "千");
            unit.Add(5, "万");
            unit.Add(6, "十");
            unit.Add(7, "百");
            unit.Add(8, "千");
            unit.Add(8, "亿");
            Dictionary<int, string> num = new Dictionary<int, string>();
            num.Add(0, "零");
            num.Add(1, "壹");
            num.Add(2, "贰");
            num.Add(3, "叁");
            num.Add(4, "肆");
            num.Add(5, "伍");
            num.Add(6, "陆");
            num.Add(7, "柒");
            num.Add(8, "捌");
            num.Add(9, "玖");
            var result = "元";//壹万元整
            bool startGreatThanZero = false, lastEqualZero = true;

            for (int i = 1; moneyInt >= 1; i++)
            {
                int temp = moneyInt % 10;
                if (temp > 0)
                {
                    if (i == 1)
                    {
                        result = num[temp] + result;
                    }
                    else
                    {
                        if (startGreatThanZero && lastEqualZero)
                        {
                            result = num[0] + result;
                        }
                        result = num[temp] + unit[i] + result;
                    }
                }
                else
                {
                    if (i == 5)
                    {
                        if (startGreatThanZero)
                        {
                            result = num[0] + result;
                            startGreatThanZero = false;
                        }
                        result = unit[i] + result;
                    }
                }
                moneyInt = moneyInt / 10;
                lastEqualZero = (temp == 0);
                startGreatThanZero = startGreatThanZero | (temp > 0);
            }
            return result;
        }
    }

}
